package com.qfedu.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.provider.entity.DbDynamics;
import com.qfedu.provider.entity.DbLike;
import com.qfedu.provider.entity.DbUserDetail;
import com.qfedu.provider.entity.DbVideo;
import com.qfedu.provider.mapper.DbDynamicsMapper;
import com.qfedu.provider.mapper.DbLikeMapper;
import com.qfedu.provider.mapper.DbUserDetailMapper;
import com.qfedu.provider.mapper.DbVideoMapper;
import com.qfedu.params.ProductionLike;
import com.qfedu.provider.service.DbLikeService;
import com.qfedu.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Administrator
* @description 针对表【db_like(点赞)】的数据库操作Service实现
* @createDate 2024-11-23 11:16:39
*/
@Service
@RequiredArgsConstructor
public class DbLikeServiceImpl extends ServiceImpl<DbLikeMapper, DbLike>
    implements DbLikeService{

    private final DbDynamicsMapper dbDynamicsMapper;
    private final DbVideoMapper dbVideoMapper;
    private final DbUserDetailMapper dbUserDetailMapper;
    private final DbLikeMapper dbLikeMapper;

    @Transactional
    @Override
    public R VideoLike(ProductionLike productionLike) {
        //检查作品状态
        DbVideo dbVideo = dbVideoMapper.selectOne(new LambdaQueryWrapper<DbVideo>().eq(DbVideo::getId, productionLike.getProductionId()).eq(DbVideo::getUserId, productionLike.getLikeUserId()));
        if (dbVideo==null){
            return R.error("作品不存在");
        }
        //检查用户状态
        DbUserDetail dbUserDetail = dbUserDetailMapper.selectOne(new LambdaQueryWrapper<DbUserDetail>().eq(DbUserDetail::getUserId, productionLike.getLikeUserId()));
        if (dbUserDetail==null){
            return R.error("用户不存在,up已经注销了账号");
        }
        if (dbUserDetail.getDeleted()!=1){
            return R.error("该用户状态异常");
        }
        //检查是否已经点赞
        DbLike dbLike = dbLikeMapper.selectOne(new LambdaQueryWrapper<DbLike>()
                .eq(DbLike::getUserId, productionLike.getUserId())
                .eq(DbLike::getProductionId, productionLike.getProductionId())
                .eq(DbLike::getProductionTypeId, productionLike.getTypeId()));
        if (dbLike==null){
            //点赞
            DbLike dbLike1 = new DbLike(productionLike.getUserId(), productionLike.getProductionId(), productionLike.getTypeId());
            //添加点赞记录
            dbLikeMapper.insert(dbLike1);
            //添加点赞记录日志
            //视频点赞数+1
            dbVideo.setLikeNumber(dbVideo.getLikeNumber()+1);
            dbVideoMapper.updateById(dbVideo);
            //用户获赞数+1,添加日志
            //结束点赞
            return R.ok("点赞成功");
        }
        if (dbLike.getStatus()==0){
            dbLike.setStatus(1);
            dbLikeMapper.updateById(dbLike);
            //添加点赞记录日志
            //视频点赞数+1
            dbVideo.setLikeNumber(dbVideo.getLikeNumber()+1);
            dbVideoMapper.updateById(dbVideo);
            //用户获赞数+1,添加日志
            //结束点赞
            return R.ok("点赞成功");

        }
        //取消点赞
        dbLike.setStatus(0);
        dbLikeMapper.updateById(dbLike);
        //视频点赞数-1
        dbVideo.setLikeNumber(dbVideo.getLikeNumber()-1);
        dbVideoMapper.updateById(dbVideo);
        //用户获赞数-1,添加日志
        return R.ok("取消点赞成功");
    }

    @Transactional
    @Override
    public R DynamicLike(ProductionLike productionLike) {
        //检查动态状态
        DbDynamics dbDynamics = dbDynamicsMapper.selectOne(new LambdaQueryWrapper<DbDynamics>().eq(DbDynamics::getId, productionLike.getProductionId()).eq(DbDynamics::getUserId, productionLike.getLikeUserId()));
        if (dbDynamics == null) {
            return R.error("动态不存在");
        }
        //检查用户状态
        DbUserDetail dbUserDetail = dbUserDetailMapper.selectOne(new LambdaQueryWrapper<DbUserDetail>().eq(DbUserDetail::getUserId, productionLike.getLikeUserId()));
        if (dbUserDetail == null) {
            return R.error("用户不存在");
        }
        if (dbUserDetail.getDeleted() != 1) {
            return R.error("该用户状态异常");
        }
        //检查是否已经点赞
        DbLike dbLike = dbLikeMapper.selectOne(new LambdaQueryWrapper<DbLike>()
                .eq(DbLike::getUserId, productionLike.getUserId())
                .eq(DbLike::getProductionId, productionLike.getProductionId())
                .eq(DbLike::getProductionTypeId, productionLike.getTypeId()));
        if (  dbLike == null) {
            //点赞
            DbLike dbLike1 = new DbLike(productionLike.getUserId(), productionLike.getProductionId(),  productionLike.getTypeId());
            //添加点赞记录
            dbLikeMapper.insert(dbLike1);
            //添加点赞记录日志
            //动态点赞数+1
            dbDynamics.setLikeNumber(dbDynamics.getLikeNumber() + 1);
            dbDynamicsMapper.updateById(dbDynamics);
            //用户获赞数+1,添加日志
            //结束点赞
            return R.ok("点赞成功");
        }
        if (dbLike.getStatus() == 0){
            dbLike.setStatus(1);
            dbLikeMapper.updateById(dbLike);
            //添加点赞记录日志
            //动态点赞数+1
            dbDynamics.setLikeNumber(dbDynamics.getLikeNumber() + 1);
            dbDynamicsMapper.updateById(dbDynamics);
            //用户获赞数+1,添加日志
            //结束点赞
            return R.ok("点赞成功");
        }
        //取消点赞
        dbLike.setStatus(0);
        dbLikeMapper.updateById(dbLike);
        //动态点赞数-1
        dbDynamics.setLikeNumber(dbDynamics.getLikeNumber() - 1);
        dbDynamicsMapper.updateById(dbDynamics);
        //用户获赞数-1,添加日志
        return R.ok("取消点赞成功");

    }


}




